package com.zby.business.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zby.business.R;
import com.zby.business.net.MyAsyncTask;
import com.zby.business.util.App;
import com.zby.business.util.Util;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.concurrent.ExecutionException;

/**
 * Created by BY on 2016/1/14.
 */
public class Login extends Activity implements View.OnClickListener {
    private Button loginButton, registerButton, exitButton;
    private ImageView backImage;
    private TextView backTextView, userTextView;
    private EditText userNameEditText, passwordEditText;
    private LinearLayout inLayout, outLayout;
    private App app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        init();
        app = (App) getApplication();
        if (!app.getName().equals("")) {
            userTextView.setText(app.getName());
            inLayout.setVisibility(View.VISIBLE);
            outLayout.setVisibility(View.INVISIBLE);
        }
    }

    private void init() {
        loginButton = (Button) findViewById(R.id.loginButton);
        registerButton = (Button) findViewById(R.id.register);
        exitButton = (Button) findViewById(R.id.exitButton);
        backImage = (ImageView) findViewById(R.id.backArrow);
        backTextView = (TextView) findViewById(R.id.backTextView);
        backImage.setOnClickListener(this);
        backTextView.setOnClickListener(this);
        loginButton.setOnClickListener(this);
        registerButton.setOnClickListener(this);
        exitButton.setOnClickListener(this);
        userTextView = (TextView) findViewById(R.id.userNameTextView);
        userNameEditText = (EditText) findViewById(R.id.userName);
        passwordEditText = (EditText) findViewById(R.id.password);
        inLayout = (LinearLayout) findViewById(R.id.inLayout);
        outLayout = (LinearLayout) findViewById(R.id.outLayout);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginButton:
                String username = userNameEditText.getText().toString().trim();
                String password = passwordEditText.getText().toString().trim();
                if (username.equals("") || password.equals("")) {
                    Util.toast(getApplicationContext(), "请填写用户名和密码");
                } else {
                    try {
                        MyAsyncTask myAsyncTask = new MyAsyncTask();
                        String s = myAsyncTask.execute("phonelogin?name=" + username + "&password=" + password).get();
                        System.out.println(s);
                        JSONObject json = new JSONObject(s);
                        String status = (String) json.get("status");
                        JSONObject jsonObject = json.getJSONObject("users");
                        if (status.equals("1")) {
                            App app = (App) getApplication();
                            String name = jsonObject.get("username") + "";
                            String id = jsonObject.get("userId") + "";
                            float account = Float.parseFloat(jsonObject.get("account") + "");
                            app.setName(name);
                            app.setUserId(id);
                            app.setAccount(account);
                            SharedPreferences sharedPreferences = getSharedPreferences("business", Activity.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("username", name);
                            editor.putString("userId", id);
                            editor.putFloat("account", account);
                            editor.commit();
                            Util.toast(getApplicationContext(), "登录成功");
                            finish();
                        } else {
                            Util.toast(getApplicationContext(), "用户名或密码错误，请重试");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.register:
                Intent intent = new Intent(getApplicationContext(), Register.class);
                startActivity(intent);
                break;
            case R.id.exitButton:
                SharedPreferences sharedPreferences = getSharedPreferences("business", Activity.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("username", "");
                editor.putString("userId", "");
                editor.commit();
                app.setUserId("");
                app.setName("");
                inLayout.setVisibility(View.INVISIBLE);
                outLayout.setVisibility(View.VISIBLE);
                break;
            case R.id.backArrow:
            case R.id.backTextView:
                finish();
                break;
        }
    }
}
